/**
 * 
 */
package com.eis.admin.mvc.bean;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eis.persist.model.function.PersistFunction;


/**
 * @author panwei01
 *
 */
public class FunctionBean implements Serializable {
	private static final long serialVersionUID = 2156729702244058321L;
	
	private static final Logger logger = LoggerFactory.getLogger(FunctionBean.class);
	private int id;
	private String functionID;
	private String functionName;
	private String functionType;
	private int typeId;
	private int status = -1;
	private String datafetchID;
	private String dataUniqueKey;
	private int bizsysId;
	private String bizsysName;
	private String bizsysConnectorStaticsClassName;
	private int connectorConfigId;
	private String connectorConfigName;
	private String beginFetchDate;
	private String beginFetchTime;

	private String clientLibraryFileName;
	private String clientClassName;
	private String connectorLibraryFileName;
	private String connectorClassName;
	
	private int layoutGroupId;
	private String layoutDefaultName;
	private int removed;
	
	public boolean isFilterable() {
		// filter fields  functionID functionDisplayName isActive functionType
		return (functionID != null && functionID.trim().length() > 0) ||
				(functionName != null && functionName.trim().length() > 0) ||
				(status >=0) || 
				(typeId >0);
	}
	
	public FunctionBean() {}
	
	public FunctionBean(PersistFunction persistFunction) {
		this.id = persistFunction.getId();
		this.functionID = persistFunction.getFunctionID();
		this.functionName = persistFunction.getFunctionDisplayName();
		this.functionType = persistFunction.getFunctionType().getTypeName();
		this.typeId = persistFunction.getFunctionType().getId();
		this.status = persistFunction.isActive() ? 1 : 0;
		this.datafetchID = persistFunction.getDatafetchID();
		this.bizsysId = persistFunction.getBizSystem().getId();
		this.bizsysName = persistFunction.getBizSystem().getName();
		this.bizsysConnectorStaticsClassName = persistFunction.getBizSystem().getConnectorStaticsClassName();
		if (persistFunction.getConnectorConfig() != null) {
			this.connectorConfigId = persistFunction.getConnectorConfig().getId();
			this.connectorConfigName = persistFunction.getConnectorConfig().getName();
		} else {
			this.connectorConfigId = 0;
		}
		
		if (persistFunction.getDataUniqueKey() != null && persistFunction.getDataUniqueKey().length > 0) {
			StringBuilder temp = new StringBuilder();
			int index =0;
			for (String oneKey : persistFunction.getDataUniqueKey()) {
				if (oneKey != null && oneKey.trim().length() > 0) {
					temp.append(oneKey);
					if (index < persistFunction.getDataUniqueKey().length - 1) {
						temp.append(",");
					}
				}
				index++;
			}
			this.dataUniqueKey = temp.toString();
		}
		if (persistFunction.getLastDataFetchTime() != null) {
			DateFormat dateformat = new SimpleDateFormat("MM/dd/yyyy");
			this.beginFetchDate = dateformat.format(persistFunction.getLastDataFetchTime());
			
			DateFormat timeformat = new SimpleDateFormat("hh:mmaa");
			this.beginFetchTime = timeformat.format(persistFunction.getLastDataFetchTime());
		}
	}
	
	public Date getLastFetchDate() {
		String lastFetch;
		DateFormat dateformat = new SimpleDateFormat("MM/dd/yyyy");
		if (this.beginFetchDate != null) {
			lastFetch = this.beginFetchDate;
		} else {
			lastFetch = dateformat.format(new Date());
		}
		
		lastFetch += " ";
		
		if (this.beginFetchTime != null) {
			lastFetch += this.beginFetchTime;
		} else {
			lastFetch += "12:00am";
		}
		DateFormat lastFetchF = new SimpleDateFormat("MM/dd/yyyy hh:mma", Locale.ENGLISH);
		try {
			return lastFetchF.parse(lastFetch);
		} catch (ParseException e) {
			logger.error("", e);
			return new Date();
		}
	}
	public String getStatusValue() {
		return this.status == 0 ? "disabled" : "enabled";
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFunctionID() {
		return functionID;
	}
	public void setFunctionID(String functionID) {
		this.functionID = functionID;
	}
	public String getFunctionName() {
		return functionName;
	}
	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}
	public String getFunctionType() {
		return functionType;
	}
	public void setFunctionType(String functionType) {
		this.functionType = functionType;
	}
	public int getTypeId() {
		return typeId;
	}
	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getDatafetchID() {
		return datafetchID;
	}
	public void setDatafetchID(String datafetchID) {
		this.datafetchID = datafetchID;
	}
	public String getDataUniqueKey() {
		return dataUniqueKey;
	}
	public void setDataUniqueKey(String dataUniqueKey) {
		this.dataUniqueKey = dataUniqueKey;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getClientLibraryFileName() {
		return clientLibraryFileName;
	}

	public void setClientLibraryFileName(String clientLibraryFileName) {
		this.clientLibraryFileName = clientLibraryFileName;
	}

	public String getClientClassName() {
		return clientClassName;
	}

	public void setClientClassName(String clientClassName) {
		this.clientClassName = clientClassName;
	}

	public String getConnectorLibraryFileName() {
		return connectorLibraryFileName;
	}

	public void setConnectorLibraryFileName(String connectorLibraryFileName) {
		this.connectorLibraryFileName = connectorLibraryFileName;
	}

	public String getConnectorClassName() {
		return connectorClassName;
	}

	public void setConnectorClassName(String connectorClassName) {
		this.connectorClassName = connectorClassName;
	}

	public int getLayoutGroupId() {
		return layoutGroupId;
	}

	public void setLayoutGroupId(int layoutGroupId) {
		this.layoutGroupId = layoutGroupId;
	}

	public String getLayoutDefaultName() {
		return layoutDefaultName;
	}

	public void setLayoutDefaultName(String layoutDefaultName) {
		this.layoutDefaultName = layoutDefaultName;
	}
	
	public String getLabel() {
		return functionName + "(" + functionID + ")";
	}

	public int getBizsysId() {
		return bizsysId;
	}

	public void setBizsysId(int bizsysId) {
		this.bizsysId = bizsysId;
	}

	public String getBizsysName() {
		return bizsysName;
	}

	public void setBizsysName(String bizsysName) {
		this.bizsysName = bizsysName;
	}

	public int getConnectorConfigId() {
		return connectorConfigId;
	}

	public void setConnectorConfigId(int connectorConfigId) {
		this.connectorConfigId = connectorConfigId;
	}

	public String getConnectorConfigName() {
		return connectorConfigName;
	}

	public void setConnectorConfigName(String connectorConfigName) {
		this.connectorConfigName = connectorConfigName;
	}

	public int getRemoved() {
		return removed;
	}

	public void setRemoved(int removed) {
		this.removed = removed;
	}

	public String getBeginFetchDate() {
		return beginFetchDate;
	}

	public void setBeginFetchDate(String beginFetchDate) {
		this.beginFetchDate = beginFetchDate;
	}

	public String getBeginFetchTime() {
		return beginFetchTime;
	}

	public void setBeginFetchTime(String beginFetchTime) {
		this.beginFetchTime = beginFetchTime;
	}

	public String getBizsysConnectorStaticsClassName() {
		return bizsysConnectorStaticsClassName;
	}

	public void setBizsysConnectorStaticsClassName(
			String bizsysConnectorStaticsClassName) {
		this.bizsysConnectorStaticsClassName = bizsysConnectorStaticsClassName;
	}
	
}